<script setup>
import { Menu } from '@element-plus/icons-vue';
import { useHomeDataStore } from '../stores/index.js';
import { useI18n } from 'vue-i18n';
import { ElMessage } from 'element-plus';
import { ref, watch } from 'vue';

const { t, locale } = useI18n();
const currentLocale = ref(locale.value);

// 监听语言变化
watch(locale, (newVal) => {
  currentLocale.value = newVal;
  console.log('语言已切换为:', newVal);
});

const getImageUrl = (user) => {
  return new URL(`../assets/images/${user}.png`, import.meta.url).href;
};

const store = useHomeDataStore();

const handleCollapse = () => {
  store.state.isCollapse = !store.state.isCollapse;
};

// 语言切换方法
const switchLanguage = (lang) => {
  locale.value = lang;
  ElMessage({
    message: t('languageChanged'),
    type: 'success',
    duration: 2000
  });
};
</script>

<template>
  <div class="header" style="width: 100%">
    <div class="l-content">
      <el-button size="small" @click="handleCollapse" style="margin-left: 10px">
        <el-icon> <Menu /> </el-icon>
      </el-button>

      <!-- 用户信息区域 -->
      <div class="user-info-header">
        <img :src="getImageUrl('user')" class="user-avatar" />
        <div class="user-details">
          <p class="user-name">admin</p>
          <p class="user-role">{{ $t('systemAdmin') }}</p>
        </div>
      </div>
    </div>
    <div class="r-content">
      <!-- 语言切换按钮 -->
      <el-button-group class="language-switcher">
        <el-button 
          :type="currentLocale === 'zh' ? 'primary' : 'default'"
          size="small"
          @click="switchLanguage('zh')"
        >
          中文
        </el-button>
        <el-button 
          :type="currentLocale === 'en' ? 'primary' : 'default'"
          size="small"
          @click="switchLanguage('en')"
        >
          English
        </el-button>
      </el-button-group>
    </div>
  </div>
</template>

<style scoped>
.header {
  height: 60px;
  background: linear-gradient(135deg, #304156 0%, #2c3e50 100%);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: relative;
}

.header::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
}

.l-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.l-content .el-button {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: white;
  border-radius: 8px;
  transition: all 0.3s ease;
}

/* 用户信息样式 */
.user-info-header {
  display: flex;
  align-items: center;
  margin-left: 16px;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.3);
  object-fit: cover;
  margin-right: 10px;
}

.user-details {
  display: flex;
  flex-direction: column;
}

.user-name {
  color: white;
  font-size: 14px;
  font-weight: 600;
  margin: 0;
  line-height: 1.2;
}

.user-role {
  color: rgba(255, 255, 255, 0.7);
  font-size: 12px;
  margin: 0;
  line-height: 1.2;
}

.r-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.language-switcher .el-button {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: white;
  transition: all 0.3s ease;
  min-width: 70px;
}

.language-switcher .el-button.is-active,
.language-switcher .el-button--primary {
  background: rgba(64, 158, 255, 0.7);
  border-color: rgba(64, 158, 255, 0.7);
  color: white;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header {
    padding: 0 16px;
  }
  
  .l-content {
    gap: 12px;
  }
  
  .r-content {
    gap: 12px;
  }
  
  .language-switcher .el-button {
    padding: 6px 12px;
    font-size: 12px;
    min-width: 60px;
  }
  
  .user-avatar {
    width: 32px;
    height: 32px;
  }
  
  .user-name {
    font-size: 13px;
  }
  
  .user-role {
    font-size: 11px;
  }
}
</style>
